let timeId = null;
Page({
  data: {
    history: [],
    historyName: [],
    hot: ['小汉堡', '农夫山泉', '牛排'],
    result: [],
    showKeywords: false,
    keywords: [],
    detail: [],
    value: '',
    showResult: false,
  },
  cancelSearch() {
    this.setData({
      showResult: false,
      showKeywords: false,
      value: ''
    })
  },
  //搜索框输入内容返回
  searchInput(options) {
    if (!options.detail.value) {
      setTimeout(this.toCommand, 500);
    } else {
      if (true) { //!this.data.showKeywords
        wx.cloud.callFunction({
            name: "searchPro",
            data: {
              value: options.detail.value
            }
          })
          .then(res => {
            console.log(res.result.data)
            var keywords = []
            var detail = []
            detail = res.result.data
            for (var x = 0; x < res.result.data.length; x++) {
              keywords.push(res.result.data[x].pname)
            }

            this.setData({
              keywords,
              detail
            })
          })
        timeId && clearTimeout(timeId);
        timeId = setTimeout(() => {
          this.setData({
            showKeywords: true
          })
        }, 0)
      }
    }
  },
  toCommand() {
    this.setData({
      keywords: [],
      showKeywords: false
    })
  },
  keywordHandle(e) {
    const text = e.target.dataset.text
    var detail = this.data.detail
    var detailtoShow = []
    for (var x = 0; x < detail.length; x++) {
      if (text == detail[x].pname) {
        detailtoShow.push(detail[x])
      }
    }

    this.setData({
      result: detailtoShow,
      value: text,
      showKeywords: false,
      showResult: true
    })
    this.historyHandle(detailtoShow);

  },

  toDetail(res) {
    wx.setStorageSync('detail', res.currentTarget.dataset)
  },
  historyHandle(value) {
    let history = this.data.history
    let historyName = this.data.historyName
    const idx = history.indexOf(value)
    var pname = value[0].pname
    var totalValue = {
      value,
      pname
    }
    var singleValue = value[0]
    if (historyName.indexOf(pname) === -1) { //如果缓存里没有
      if (history.length > 7) {
        history.pop()
        historyName.pop()
      }
      history.unshift(singleValue);
      historyName.unshift(pname)
      wx.setStorageSync('history', JSON.stringify(history))
      wx.setStorageSync('historyName', historyName)
      this.setData({
        history,
        historyName
      });
    }
  },
  visitHistory(e){
    wx.cloud.callFunction({
      name: "getProwithHis",
      data: {
        pname: e.currentTarget.dataset.text
      }
    }).then(res=>{
      console.log(res.result.data)
      this.setData({
        result:res.result.data,
        showResult:true
      })
    })
  },
  onLoad() {
    const history = wx.getStorageSync('history');
    const historyName = wx.getStorageSync('historyName')
    if (history) {
      this.setData({
        history: JSON.parse(history),
      })
    }
    if (historyName) {
      this.setData({
        historyName
      })
    }
  }
})